package cn.com.pxto.commons;

import java.util.ArrayList;


/**
 * ��������࣬����sql���ҵ�ʱ�򣬸�ݲ�������������sqlƬ��
 * @author Gauss Cheung
 *
 */
public class SearchParam {
	public static final int SEARCH_PARAM_TYPE_NUMBER = 1;  //���ִ���ȷ����
	public static final int SEARCH_PARAM_TYPE_STRING_DISTINCT = 2; //�ַ�ȷ����
	public static final int SEARCH_PARAM_TYPE_STRING_INDISTINCT = 3; //�ַ�ģ�����
	public static final int SEARCH_PARAM_TYPE_GREATER_OR_EQUAL=4;//���־�ȷ���ң����ڵ���
	public static final int SEARCH_PARAM_TYPE_LESS_OR_EQUAL=5;//���־�ȷ���ң�С�ڵ���
	public static final int SEARCH_PARAM_TYPE_UNEQUAL=6;//���־�ȷ���ң�������
	public static final int SEARCH_PARAM_TYPE_IN = 7; //��
	public static final int SEARCH_PARAM_TYPE_STRING_HAFEINDISTINCT = 8;//�ַ��ģ����ң�ֻ�������ַ�ͨ���
	public static final int SEARCH_PARAM_TYPE_BETWEEN = 9;
	private String condition;
	private Object value;
	private int type;
	
	public String getCondition() {
		return condition;
	}
	public void setCondition(String condition) {
		if (condition==null) 
			throw new IllegalArgumentException("Can not set condition to null");
		else 
			this.condition = condition;
	}
	public Object getValue() {
		return value;
	}
	public void setValue(Object value) {
		this.value = value;
	}
	public int getType() {
		return type;
	}
	public void setType(int type) {
		this.type = type;
	}
	/**
	 * ������ѯ����
	 * @param condition ��ѯ���
	 * @param value
	 * @param type
	 */
	public SearchParam(String condition,Object value,int type){
		this.condition = condition;
		this.value = value;
		this.type = type;
	}
	/**
	 * ��ɼ򵥵�and��ϵ�Ĳ�ѯwhere�Ӿ�����
	 * @param params SearchParam��������
	 * @param hqlObjectName hql�д����Ķ���
	 * @return String where��ѯ�Ӿ�
	 */
	public static String getSimpleWhereSentence(SearchParam[] params,String hqlObjectName){
		StringBuffer result  = new StringBuffer();
		for (int i = 0; i < params.length; i++) {
			SearchParam sp = params[i];
			String operation = "=";
			String value = "?";
			switch (sp.getType()){  //����switch �Ա�)չ
			case SEARCH_PARAM_TYPE_STRING_INDISTINCT:
				operation = " like ";
				sp.setValue("%"+sp.getValue()+"%");
				break;
			case SEARCH_PARAM_TYPE_GREATER_OR_EQUAL:
				operation=" >= ";
				break;
			case SEARCH_PARAM_TYPE_LESS_OR_EQUAL:
				operation=" <= ";
				break;
			case SEARCH_PARAM_TYPE_UNEQUAL:
				operation=" <> ";
				break;	
			case SEARCH_PARAM_TYPE_IN:
				operation = " in ";
				value = "("+params[i].value+") ";
				break;
			case SEARCH_PARAM_TYPE_BETWEEN :
				operation = " between ";
				Object[] betweenDate = (Object[])params[i].value;
				value = betweenDate[0]+ " and " + betweenDate[1];
				break;
			case SEARCH_PARAM_TYPE_STRING_HAFEINDISTINCT:
				operation = " like ";
				break;
				
			}
			
			result.append(" and "+hqlObjectName+"."+ sp.getCondition() + operation + value);
		}
		return result.toString();
	}
	/**
	 * ����ѯ���������е�value�������һ�����飬�ṩ���ѯʹ��
	 * @param params ��ѯ��������
	 * @return values Object����
	 */
	public static Object[] getSearchValues(SearchParam[] params){
		//Object[] result = new Object[params.length];
		ArrayList result = new ArrayList();
		for (int i=0;i<params.length;i++){
			if (params[i].getType()!=SEARCH_PARAM_TYPE_IN && params[i].getType()!=SEARCH_PARAM_TYPE_BETWEEN)
				result.add(params[i].getValue());
		}
		return result.toArray();
	}
}
